home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / t_unix / j109lxa4.tar / ax25.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  24KB  |  849 lines

  1. /*  Low level AX.25 code:
  2.  *  incoming frame processing (including digipeating)
  3.  *  IP encapsulation
  4.  *  digipeater routing
  5.  *
  6.  *  Copyright 1991 Phil Karn, KA9Q / 1991 Kevin Hil, G1EMM
  7.  */
  8. /* Mods by PA0GRI */
  9.  
  10. #include <stdio.h>
  11. #include "global.h"
  12. #include "config.h"
  13. #ifdef AX25
  14. #include "mbuf.h"
  15. #include "iface.h"
  16. #include "arp.h"
  17. #include "slip.h"
  18. #include "ax25.h"
  19. #include "lapb.h"
  20. #include "netrom.h"
  21. #include "ip.h"
  22. #include "devparam.h"
  23. #include "trace.h"
  24. #include "pktdrvr.h"
  25. #include "netuser.h"
  26. #include "commands.h"
  27. #include <ctype.h>
  28.  
  29. static int axsend __ARGS((struct iface *iface,char *dest,char *source,
  30.     int cmdrsp,int ctl,struct mbuf *data));
  31. static int axip_stop __ARGS((struct iface *iface));
  32. static int axip_raw __ARGS((struct iface *iface,struct mbuf *bp));
  33.  
  34. /* List of AX.25 multicast addresses in network format (shifted ascii).
  35.  * Only the first entry is used for transmissions, but any incoming
  36.  * packet with any one of these destination addresses is recognized
  37.  * as a multicast
  38.  */
  39. /* NOTE: IF you CHANGE the order of these, also change the codes in ax25.h !!!
  40.  * mailfor.c, nr3, and ax25cmd.c depend on this to get broadcast addresses !!!
  41.  * 920306 - WG7J
  42.  */
  43. char Ax25multi[][AXALEN] = {
  44.     'Q'<<1, 'S'<<1, 'T'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* QST */
  45.     'N'<<1, 'O'<<1, 'D'<<1, 'E'<<1, 'S'<<1, ' '<<1, '0'<<1, /* NODES */
  46.     'M'<<1, 'A'<<1, 'I'<<1, 'L'<<1, ' '<<1, ' '<<1, '0'<<1, /* MAIL */
  47.     'I'<<1, 'D'<<1, ' '<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* ID */
  48. #ifdef notdef
  49.     'O'<<1, 'P'<<1, 'E'<<1, 'N'<<1, ' '<<1, ' '<<1, '0'<<1, /* OPEN */
  50.     'C'<<1, 'Q'<<1, ' '<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* CQ */
  51.     'B'<<1, 'E'<<1, 'A'<<1, 'C'<<1, 'O'<<1, 'N'<<1, '0'<<1, /* BEACON */
  52.     'R'<<1, 'M'<<1, 'N'<<1, 'C'<<1, ' '<<1, ' '<<1, '0'<<1, /* RMNC */
  53.     'A'<<1, 'L'<<1, 'L'<<1, ' '<<1, ' '<<1, ' '<<1, '0'<<1, /* ALL */
  54. #endif
  55.     '\0',
  56. };
  57. char Mycall[AXALEN];
  58. char Myalias[AXALEN];    /* the NETROM alias in 'call' form */
  59. #ifdef NETROM
  60. char Nralias[ALEN+1];      /* the NETROM alias in 'alias' form */
  61. #endif
  62. #ifdef CONVERS
  63. char Ccall[AXALEN];
  64. #endif
  65. #ifdef TTYCALL
  66. char Ttycall[AXALEN];
  67. #endif
  68.  
  69. struct ax_route *Ax_routes; /* Routing table header */
  70. #ifdef AXIP
  71. static int32 axipaddr[NAX25];    /* table of IP addresses of AX.25 interfaces */
  72. #ifdef PPP
  73. extern int16 DFAR fcstab[];      /* table used when calculating FCS */
  74. #else
  75. int16 fcstab[256] = {
  76.     0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  77.     0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  78.     0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  79.     0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  80.     0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  81.     0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  82.     0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  83.     0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  84.     0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  85.     0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  86.     0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  87.     0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  88.     0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  89.     0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  90.     0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  91.     0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  92.     0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  93.     0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  94.     0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  95.     0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  96.     0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  97.     0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  98.     0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  99.     0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  100.     0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  101.     0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  102.     0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  103.     0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  104.     0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  105.     0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  106.     0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  107.     0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  108. };
  109. #endif
  110. #endif
  111.  
  112. /* Send IP datagrams across an AX.25 link */
  113. int
  114. ax_send(bp,iface,gateway,prec,del,tput,rel)
  115. struct mbuf *bp;
  116. struct iface *iface;
  117. int32 gateway;
  118. int prec;
  119. int del;
  120. int tput;
  121. int rel;
  122. {
  123.     char *hw_addr;
  124.     struct ax25_cb *axp;
  125.     struct mbuf *tbp;
  126.  
  127.     struct ax_route *axr;
  128.     char mode = AX_DEFMODE;        /* default to interface mode */
  129.  
  130.     if(gateway == iface->broadcast) /* This is a broadcast IP datagram */
  131.          return (*iface->output)(iface,Ax25multi[0],iface->hwaddr,PID_IP,bp);
  132.  
  133.     if((hw_addr = res_arp(iface,ARP_AX25,gateway,bp)) == NULLCHAR)
  134.         return 0;    /* Wait for address resolution */
  135.  
  136.     /* If there's a defined route, get it */
  137.     axr = ax_lookup(hw_addr,iface);
  138.  
  139.     if(axr == NULLAXR) {
  140.         if(iface->flags & CONNECT_MODE){
  141.             mode = AX_VC_MODE;
  142.         } else {
  143.             mode = AX_DATMODE;
  144.         }
  145.     } else {
  146.         mode = axr->mode;
  147.         if(mode == AX_DEFMODE) {
  148.             if(iface->flags & CONNECT_MODE){
  149.                 mode = AX_VC_MODE;
  150.             } else {
  151.                 mode = AX_DATMODE;
  152.             }
  153.         }    
  154.     }
  155.  
  156.     /* UI frames are used for any one of the following three conditions:
  157.      * 1. The "low delay" bit is set in the type-of-service field.
  158.      * 2. The "reliability" TOS bit is NOT set and the interface is in
  159.      *    datagram mode.
  160.      * 3. The destination is the broadcast address (this is helpful
  161.      *    when broadcasting on an interface that's in connected mode).
  162.      */
  163.     if(del || (!rel && (mode == AX_DATMODE))
  164.      || addreq(hw_addr,Ax25multi[0])){
  165.         /* Use UI frame */
  166.         return (*iface->output)(iface,hw_addr,iface->hwaddr,PID_IP,bp);
  167.     }
  168.  
  169.  
  170.     /* Reliability is needed; use I-frames in AX.25 connection */
  171.     if((axp = find_ax25(iface->hwaddr,hw_addr,iface)) == NULLAX25){
  172.         /* Open a new connection */
  173.         axp = open_ax25(iface,iface->hwaddr,hw_addr,
  174.          AX_ACTIVE,Axwindow,s_arcall,s_atcall,s_ascall,-1);
  175.         if(axp == NULLAX25){
  176.             free_p(bp);
  177.             return -1;
  178.         }
  179.     }
  180.     if(axp->state == LAPB_DISCONNECTED){
  181.         est_link(axp);
  182.         lapbstate(axp,LAPB_SETUP);
  183.     }
  184.     /* Insert the PID */
  185.     if((tbp = pushdown(bp,1)) == NULLBUF){
  186.         free_p(bp);
  187.         return -1;
  188.     }
  189.     bp = tbp;
  190.     bp->data[0] = PID_IP;
  191.     if((tbp = segmenter(bp,axp->paclen)) == NULLBUF){
  192.         free_p(bp);
  193.         return -1;
  194.     }
  195.     return send_ax25(axp,tbp,-1);
  196. }
  197. /* Add header and send connectionless (UI) AX.25 packet.
  198.  * Note that the calling order here must match enet_output
  199.  * since ARP also uses it.
  200.  */
  201. int
  202. ax_output(iface,dest,source,pid,data)
  203. struct iface *iface;    /* Interface to use; overrides routing table */
  204. char *dest;        /* Destination AX.25 address (7 bytes, shifted) */
  205. char *source;        /* Source AX.25 address (7 bytes, shifted) */
  206. int16 pid;        /* Protocol ID */
  207. struct mbuf *data;    /* Data field (follows PID) */
  208. {
  209.     struct mbuf *bp;
  210.  
  211.     /* Prepend pid to data */
  212.     bp = pushdown(data,1);
  213.     if(bp == NULLBUF){
  214.         free_p(data);
  215.         return -1;
  216.     }
  217.     bp->data[0] = (char)pid;
  218.     return axsend(iface,dest,source,LAPB_COMMAND,UI,bp);
  219. }
  220. /* Common subroutine for sendframe() and ax_output() */
  221. static int
  222. axsend(iface,dest,source,cmdrsp,ctl,data)
  223. struct iface *iface;    /* Interface to use; overrides routing table */
  224. char *dest;        /* Destination AX.25 address (7 bytes, shifted) */
  225. char *source;        /* Source AX.25 address (7 bytes, shifted) */
  226. int cmdrsp;        /* Command/response indication */
  227. int ctl;        /* Control field */
  228. struct mbuf *data;    /* Data field (includes PID) */
  229. {
  230.     struct mbuf *cbp;
  231.     struct ax25 addr;
  232.     struct ax_route *axr;
  233.     char *idest;
  234.     int rval;
  235.  
  236.     /* If the source addr is unspecified, use the interface address */
  237.     if(source[0] == '\0')
  238.         source = iface->hwaddr;
  239.  
  240.     /* If there's a digipeater route, get it */
  241.     axr = ax_lookup(dest,iface);
  242.  
  243.     memcpy(addr.dest,dest,AXALEN);
  244.     memcpy(addr.source,source,AXALEN);
  245.     addr.cmdrsp = cmdrsp;
  246.  
  247.     if(axr != NULLAXR){
  248.         memcpy(addr.digis,axr->digis,(size_t)axr->ndigis*AXALEN);
  249.         addr.ndigis = axr->ndigis;
  250.         idest = addr.digis[0];
  251.     } else {
  252.         addr.ndigis = 0;
  253.         idest = dest;
  254.     }
  255.  
  256.     addr.nextdigi = 0;
  257.  
  258.     /* Allocate mbuf for control field, and fill in */
  259.     if((cbp = pushdown(data,1)) == NULLBUF){
  260.         free_p(data);
  261.         return -1;
  262.     }
  263.     cbp->data[0] = ctl;
  264.  
  265.     if((data = htonax25(&addr,cbp)) == NULLBUF){
  266.         free_p(cbp);    /* Also frees data */
  267.         return -1;
  268.     }
  269.     /* This shouldn't be necessary because redirection has already been
  270.      * done at the IP router layer, but just to be safe...
  271.      */
  272.     if(iface->forw != NULLIF){
  273. #ifdef notdef
  274.         logsrc(iface->forw,iface->forw->hwaddr);
  275. #endif
  276.         logsrc(iface->forw,source);
  277.         logdest(iface->forw,idest);
  278.         rval = (*iface->forw->raw)(iface->forw,data);
  279.     } else {
  280. #ifdef notdef
  281.         logsrc(iface,iface->hwaddr);
  282. #endif
  283.         logsrc(iface,source);
  284.         logdest(iface,idest);
  285.         rval = (*iface->raw)(iface,data);
  286.     }
  287.     return rval;
  288. }
  289. #ifdef    AXIP
  290. /* Handle AX.25 frames received inside IP according to RFC-1226 */
  291. void
  292. axip_input(iface,ip,bp,rxbroadcast)
  293. struct iface *iface;    /* Input interface */
  294. struct ip *ip;          /* IP header */
  295. struct mbuf *bp;        /* AX.25 frame with FCS */
  296. int rxbroadcast;    /* Accepted for now */
  297. {
  298.     int i;
  299.     struct mbuf *tbp;
  300.     int16 len, f, fcs = 0xffff;
  301.  
  302.     /* Since the AX.25 frame arrived on an interface that does
  303.        not necessarily support AX.25, we have to find a suitable
  304.        AX.25 interface, or drop the packet.
  305.      */
  306.     /* Try to find a matching AX.25 pseudo interface */
  307.     for(i=0; i < NAX25; ++i)
  308.          if(axipaddr[i] == ip->source)
  309.           break;
  310.     if(i == NAX25) {
  311.          /* Here we could still try to pick a real AX.25 interface,
  312.         but that would mean that we are accepting AX.25 frames
  313.         from unknown IP hosts, so we'd rather drop it.
  314.           */
  315.          free_p(bp);
  316.          return;
  317.     }
  318.     iface = Ifaces;
  319.     while (iface != NULLIF) {
  320.          if(iface->raw == axip_raw && iface->dev == i)
  321.           /* found the right AX.25 pseudo interface */
  322.           break;
  323.          iface = iface->next;
  324.     }
  325.     if(iface == NULLIF) {
  326.          free_p(bp);
  327.          return;
  328.     }
  329.     len = len_p(bp) - sizeof(fcs);
  330.     if(dup_p(&tbp,bp,0,len) != len) {
  331.          free_p(bp);
  332.          return;
  333.     }
  334.     while(len--)
  335.          fcs = (fcs >> 8) ^ fcstab[(fcs ^ PULLCHAR(&bp)) & 0x00ff];
  336.     fcs ^= 0xffff;
  337.     f = PULLCHAR(&bp);
  338.     f |= (PULLCHAR(&bp) << 8);
  339.     if(fcs == f){
  340.          /* add some tracing - WG7J */
  341. #ifdef TRACE
  342.         dump(iface,IF_TRACE_IN,CL_AX25,tbp);
  343. #endif
  344.          ax_recv(iface,tbp);
  345.     } else
  346.          free_p(tbp);
  347. }
  348. #endif
  349. /* Process incoming AX.25 packets.
  350.  * After optional tracing, the address field is examined. If it is
  351.  * directed to us as a digipeater, repeat it.  If it is addressed to
  352.  * us or to QST-0, kick it upstairs depending on the protocol ID.
  353.  */
  354. extern int32 CT4init;
  355.  
  356. void
  357. ax_recv(iface,bp)
  358. struct iface *iface;
  359. struct mbuf *bp;
  360. {
  361.     struct mbuf *hbp;
  362.     char control;
  363.     struct ax25 hdr;
  364.     struct ax25_cb *axp;
  365.     struct ax_route *axr;
  366.     struct iface *cross;
  367.     char (*mpp)[AXALEN];
  368.     int mcast;
  369.     char *isrc,*idest;  /* "immediate" source and destination */
  370.     int To_us = 0;      /* Is this a link to us ? */
  371.  
  372.     /* Pull header off packet and convert to host structure */
  373.     if(ntohax25(&hdr,&bp) < 0){
  374.         /* Something wrong with the header */
  375.         free_p(bp);
  376.         return;
  377.     }
  378.  
  379.     if(iface->flags & LOG_IPHEARD) {
  380.         struct mbuf *nbp;
  381.         struct ip ip;
  382.         int len;
  383.  
  384.         len = len_p(bp);
  385.         if(dup_p(&nbp, bp, 0, len) == len) {
  386.             /* find higher proto, if any */
  387.             (void) PULLCHAR(&nbp);  /* skip control byte */
  388.             if(PULLCHAR(&nbp) == PID_IP) {
  389.                 ntohip(&ip,&nbp);
  390.                 if(ip.version == IPVERSION)
  391.                     log_ipheard(ip.source,iface);
  392.             }
  393.         free_p(nbp);
  394.         }
  395.     }
  396.  
  397.     /* If there were digis in this packet and at least one has
  398.      * been passed, then the last passed digi is the immediate source.
  399.      * Otherwise it is the original source.
  400.      */
  401.     if(hdr.ndigis != 0 && hdr.nextdigi != 0)
  402.         isrc = hdr.digis[hdr.nextdigi-1];
  403.     else
  404.         isrc = hdr.source;
  405.  
  406.     /* If there are digis in this packet and not all have been passed,
  407.      * then the immediate destination is the next digi. Otherwise it
  408.      * is the final destination.
  409.      */
  410.     cross = NULLIF;
  411.     if(hdr.ndigis != 0 && hdr.nextdigi != hdr.ndigis){
  412.         idest = hdr.digis[hdr.nextdigi];
  413.         if(!addreq(idest,iface->hwaddr)){
  414.             /* Check if digi matches callsign of any other
  415.              * interface for crossband digipeating.
  416.              */
  417.             for(cross = Ifaces; cross != NULLIF; cross = cross->next){
  418.                 if(cross->type == CL_AX25 &&
  419.                     addreq(idest,cross->hwaddr)) {
  420.                     /* Swap callsigns so that the reply
  421.                      * can be crossband digipeated in
  422.                      * the other direction.
  423.                      */
  424.                     memcpy(idest,iface->hwaddr,AXALEN);
  425.                     break;
  426.                 }
  427.             }
  428.         }
  429.     }
  430.     else
  431.         idest = hdr.dest;
  432.  
  433.     /* Don't log our own packets if we overhear them, as they're
  434.      * already logged by axsend() and by the digipeater code.
  435.      */
  436. #ifdef NETROM
  437.     if(!addreq(isrc,iface->hwaddr) && !addreq(isrc,Nr_iface->hwaddr)){
  438. #else    
  439.     if(!addreq(isrc,iface->hwaddr)){
  440. #endif
  441.         logsrc(iface,isrc);
  442.         logdest(iface,idest);
  443.     }
  444.     /* Examine immediate destination for a multicast address */
  445.     mcast = 0;
  446.     for(mpp = Ax25multi;(*mpp)[0] != '\0';mpp++){
  447.         if(addreq(idest,*mpp)){
  448.             mcast = 1;
  449.             break;
  450.         }
  451.     }
  452.  
  453.     /* Now check for any connection already in the AX.25 cb list
  454.      * This allows netrom user connects (with inverted ssid's) ,
  455.      * connections already established etc.. to pass.
  456.      * There should be no more digis needed!
  457.      * Added 11/15/91 WG7J
  458.      */
  459. #ifdef __GNUC__
  460.     axp = 0;        /* silence warning */
  461. #endif
  462.     if(hdr.nextdigi == hdr.ndigis)
  463.         /* See if the source and destination address are in hash table */
  464.         if((axp = find_ax25(hdr.dest,hdr.source,iface)) != NULLAX25)
  465.             To_us = KNOWN_LINK;
  466.  
  467.     /* Check to see if this is the Netrom interface callsign
  468.      * or the alias ! Accept any SSID on the alias.
  469.      * NOTE: This also allows digipeating via those calls!
  470.      * Added 11/15/91 WG7J
  471.      */
  472.     if(!To_us) {
  473.         if(!memcmp(idest,Myalias,ALEN))
  474.             To_us = ALIAS_LINK;     /* this is for the alias */
  475.         else
  476. #ifdef NETROM
  477.           if((iface->flags & IS_NR_IFACE) && addreq(idest,Nr_iface->hwaddr))
  478.             To_us = NETROM_LINK;    /* this is for the netrom callsign! */
  479.         else
  480. #endif
  481.           if(addreq(idest,iface->hwaddr))
  482.             To_us = IFACE_LINK;     /* this is to the interface call */
  483. #ifdef CONVERS
  484.         else if(addreq(idest,Ccall) && (iface->flags & IS_CONV_IFACE))
  485.             To_us = CONF_LINK;      /* this is for the conference call */
  486. #endif
  487. #ifdef TTYCALL
  488.     else if (addreq(idest, Ttycall))
  489.         To_us = TTY_LINK;
  490. #endif
  491.     }
  492.  
  493.     if(!To_us && !mcast && !cross){
  494.         /* Not a broadcast, and not addressed to us. Inhibit
  495.          * transmitter to avoid colliding with addressed station's
  496.          * response, and discard packet.
  497.          */
  498. #ifdef notdef
  499.         if(iface->ioctl != NULL){
  500.             (*iface->ioctl)(iface,PARAM_MUTE,1,-1L);
  501.         }
  502. #endif
  503.         free_p(bp);
  504.         return;
  505.     }
  506.  
  507. #ifdef notdef
  508.     if(!mcast && !cross && iface->ioctl != NULL){
  509.         /* Packet was sent to us; abort transmit inhibit */
  510.         (*iface->ioctl)(iface,PARAM_MUTE,1,0L);
  511.     }
  512. #endif
  513.     /* At this point, packet is either addressed to us, or is
  514.      * a multicast.
  515.      */
  516.     if(hdr.nextdigi < hdr.ndigis){
  517.         /* Packet requests digipeating. See if we can repeat it. */
  518.         if((iface->flags & AX25_DIGI) && !mcast){
  519.             /* Yes, kick it back out. htonax25 will set the
  520.              * repeated bit.
  521.              */
  522.             hdr.nextdigi++;
  523.             if(cross)   /* Crossband digipeat */
  524.                 iface = cross;
  525.             if(iface->flags & AX25_DIGI) {
  526.                 if((hbp = htonax25(&hdr,bp)) != NULLBUF){
  527.                     logsrc(iface,iface->hwaddr);
  528.                     if(iface->forw != NULLIF){
  529.                         logdest(iface->forw,hdr.digis[hdr.nextdigi]);
  530.                         (*iface->forw->raw)(iface->forw,hbp);
  531.                     } else {
  532.                         logdest(iface,hdr.digis[hdr.nextdigi]);
  533.                         (*iface->raw)(iface,hbp);
  534.                     }
  535.                     bp = NULLBUF;
  536.                 }
  537.             }
  538.         }
  539.         free_p(bp);    /* Dispose if not forwarded */
  540.         return;
  541.     }
  542.     /* If we reach this point, then the packet has passed all digis,
  543.      * and is either addressed to us or is a multicast
  544.      */
  545.     if(bp == NULLBUF){
  546.         return;         /* Nothing left */
  547.     }
  548.  
  549.     /* If there's no locally-set entry in the routing table and
  550.      * this packet has digipeaters, create or update it. Leave
  551.      * local routes alone.
  552.      */
  553.     if(((axr = ax_lookup(hdr.source,iface)) == NULLAXR || axr->type == AX_AUTO)
  554.      && hdr.ndigis > 0){
  555.         char digis[MAXDIGIS][AXALEN];
  556.         int i,j;
  557.  
  558.         /* Construct reverse digipeater path */
  559.         for(i=hdr.ndigis-1,j=0;i >= 0;i--,j++){
  560.             memcpy(digis[j],hdr.digis[i],AXALEN);
  561.             digis[j][ALEN] &= ~(E|REPEATED);
  562.         }
  563.         ax_add(hdr.source,AX_AUTO,digis,hdr.ndigis,iface);
  564.     }
  565.     /* Sneak a peek at the control field. This kludge is necessary because
  566.      * AX.25 lacks a proper protocol ID field between the address and LAPB
  567.      * sublayers; a control value of UI indicates that LAPB is to be
  568.      * bypassed.
  569.      */
  570.     control = *bp->data & ~PF;
  571.  
  572.     if(uchar(control) == UI){
  573.         int pid;
  574.         struct axlink *ipp;
  575.  
  576.         (void) PULLCHAR(&bp);
  577.         if((pid = PULLCHAR(&bp)) == -1)
  578.             return;        /* No PID */
  579.         /* Find network level protocol and hand it off */
  580.         for(ipp = Axlink;ipp->funct != NULL;ipp++){
  581.             if(ipp->pid == pid)
  582.                 break;
  583.         }
  584.         if(ipp->funct != NULL)
  585.             (*ipp->funct)(iface,NULLAX25,hdr.source,hdr.dest,bp,mcast);
  586.         else
  587.             free_p(bp);
  588.         return;
  589.     }
  590.     /* Everything from here down is connected-mode LAPB, so ignore
  591.      * multicasts
  592.      */
  593.     if(mcast){
  594.         free_p(bp);
  595.         return;
  596.     }
  597.     /* At this point, if we already have a connection on its way,
  598.      * we already have found the control block !
  599.      * 11/15/91 WG7J/PA3DIS
  600.      */
  601.     if(To_us !=KNOWN_LINK) {
  602.         /* This is a new connection to either the interface call,
  603.          * or to the netrom-interface call or system alias.
  604.          * Create a new ax25 entry for this guy,
  605.          * insert into hash table keyed on his address,
  606.          * and initialize table entries
  607.          */
  608.         if((axp = cr_ax25(hdr.dest,hdr.source,iface)) == NULLAX25){
  609.             free_p(bp);
  610.             return;
  611.         }
  612. #ifdef CONVERS
  613.         /* set a different T4 if conference link - WG7J */
  614.         if(To_us == CONF_LINK)
  615.             set_timer(&axp->t4,CT4init * 1000L);
  616. #endif
  617.         axp->jumpstarted = To_us;
  618.         if(hdr.cmdrsp == LAPB_UNKNOWN)
  619.             axp->proto = V1;    /* Old protocol in use */
  620.     }
  621.     lapb_input(axp,hdr.cmdrsp,bp);
  622. }
  623.  
  624. /* General purpose AX.25 frame output */
  625. int
  626. sendframe(axp,cmdrsp,ctl,data)
  627. struct ax25_cb *axp;
  628. int cmdrsp;
  629. int ctl;
  630. struct mbuf *data;
  631. {
  632.     return axsend(axp->iface,axp->remote,axp->local,cmdrsp,ctl,data);
  633. }
  634. /* Find a route for an AX.25 address.
  635.  * Code to remove SSID field C/R- and E-bits in ax_lookup(), ax_add() and
  636.  * ax_drop() added by vk6zjm 4/5/92. This eliminates duplicate AX25 routes.
  637.  * 1992-05-28 - Added interface -- sm6rpz
  638.  */
  639.  
  640. struct ax_route *
  641. ax_lookup(target,ifp)
  642. char *target;
  643. struct iface *ifp;
  644. {
  645.     register struct ax_route *axr;
  646.     struct ax_route *axlast = NULLAXR;
  647.     char xtarget[AXALEN];
  648.  
  649.     /* Remove C/R and E bits in local copy only */
  650.     memcpy(xtarget,target,AXALEN);
  651.     xtarget[AXALEN-1] &= SSID;
  652.  
  653.     for(axr = Ax_routes; axr != NULLAXR; axlast = axr,axr = axr->next){
  654.         if(memcmp(axr->target,xtarget,AXALEN) == 0 && axr->iface == ifp){
  655.             if(axr != Ax_routes){
  656.                 /* Move entry to top of list to speed
  657.                  * future searches
  658.                  */
  659.                 axlast->next = axr->next;
  660.                 axr->next = Ax_routes;
  661.                 Ax_routes = axr;
  662.  
  663.             }
  664.             return axr;
  665.         }
  666.     }
  667.     return axr;
  668. }
  669. /* Add an entry to the AX.25 routing table */
  670. struct ax_route *
  671. ax_add(target,type,digis,ndigis,ifp)
  672. char *target;
  673. int type;
  674. char digis[][AXALEN];
  675. int ndigis;
  676. struct iface *ifp;
  677. {
  678.     register struct ax_route *axr;
  679.     char xtarget[AXALEN];
  680.  
  681.     if(ndigis < 0 || ndigis > MAXDIGIS || addreq(target,ifp->hwaddr))
  682.         return NULLAXR;
  683.  
  684.     /* Remove C/R and E bits in local copy only */
  685.     memcpy(xtarget,target,AXALEN);
  686.     xtarget[AXALEN-1] &= SSID;
  687.  
  688.     if((axr = ax_lookup(xtarget,ifp)) == NULLAXR){
  689.         axr = (struct ax_route *)callocw(1,sizeof(struct ax_route));
  690.         axr->next = Ax_routes;
  691.         Ax_routes = axr;
  692.         memcpy(axr->target,xtarget,AXALEN);
  693.         axr->ndigis = ndigis;
  694.         axr->iface = ifp;
  695.     }
  696.     axr->type = type;
  697.     axr->mode = AX_DEFMODE;        /* set mode to default */
  698.     if(axr->ndigis != ndigis)
  699.         axr->ndigis = ndigis;
  700.  
  701.     memcpy(axr->digis,digis[0],(size_t)ndigis*AXALEN);
  702.     return axr;
  703. }
  704. int
  705. ax_drop(target,ifp)
  706. char *target;
  707. struct iface *ifp;
  708. {
  709.     register struct ax_route *axr;
  710.     struct ax_route *axlast = NULLAXR;
  711.     char xtarget[AXALEN];
  712.  
  713.     /* Remove C/R and E bits in local copy only */
  714.     memcpy(xtarget,target,AXALEN);
  715.     xtarget[AXALEN-1] &= SSID;
  716.  
  717.     for(axr = Ax_routes;axr != NULLAXR;axlast=axr,axr = axr->next)
  718.         if(memcmp(axr->target,xtarget,AXALEN) == 0 && axr->iface == ifp)
  719.             break;
  720.     if(axr == NULLAXR)
  721.         return -1;    /* Not in table! */
  722.     if(axlast != NULLAXR)
  723.         axlast->next = axr->next;
  724.     else
  725.         Ax_routes = axr->next;
  726.  
  727.     free((char *)axr);
  728.     return 0;
  729. }
  730. /* Handle ordinary incoming data (no network protocol) */
  731. void
  732. axnl3(iface,axp,src,dest,bp,mcast)
  733. struct iface *iface;
  734. struct ax25_cb *axp;
  735. char *src;
  736. char *dest;
  737. struct mbuf *bp;
  738. int mcast;
  739. {
  740.     if(axp == NULLAX25){
  741.         free_p(bp);
  742.         /* beac_input(iface,src,bp); */
  743.     } else {
  744.         append(&axp->rxq,bp);
  745.         if(axp->r_upcall != NULLVFP((struct ax25_cb*,int)))
  746.             (*axp->r_upcall)(axp,len_p(axp->rxq));
  747.     }
  748. }
  749.  
  750. #ifdef    AXIP
  751. /* attach a fake AX.25 interface for AX.25 on top of IP */
  752. /* argv[0] == "axip"
  753.  * argv[1] == name of new interface
  754.  * argv[2] == MTU
  755.  * argv[3] == hostname of remote end of wormhole
  756.  * argv[4] == optional AX.25 callsign for this interface, must be specified
  757.  *          and be different from any other interface callsign if crossband
  758.  *             digipeating is going to work properly
  759.  */
  760. int
  761. axip_attach(argc,argv,p)
  762. int argc;
  763. char *argv[];
  764. void *p;
  765. {
  766.      int i;
  767.      struct iface *ifp;
  768.  
  769.      /* Check for too long iface names - WG7J */
  770.      if(strlen(argv[1]) >= ILEN) {
  771.         tprintf("interface max %d chars\n",ILEN-1);
  772.         return -1;
  773.      }
  774.      if(if_lookup(argv[1]) != NULLIF) {
  775.       tprintf("interface %s already attached\n",argv[1]);
  776.       return -1;
  777.      }
  778.      for(i=0; i < NAX25; ++i)
  779.       if(axipaddr[i] == 0)
  780.            break;
  781.      if(i == NAX25) {
  782.       tprintf("too many AX25 interfaces attached\n");
  783.       return -1;
  784.      }
  785.      if((axipaddr[i] = resolve(argv[3])) == 0) {
  786.       tprintf("invalid address\n");
  787.       return -1;
  788.      }
  789.      ifp = (struct iface *)callocw(1,sizeof(struct iface));
  790.      ifp->dev = i;
  791.      ifp->addr = Ip_addr;
  792.      ifp->name = strdup(argv[1]);
  793.      ifp->hwaddr = mallocw(AXALEN);
  794.      memcpy(ifp->hwaddr,Mycall,AXALEN);
  795.      ifp->mtu = atoi(argv[2]);
  796.      setencap(ifp,"AX25");
  797.      if(argc > 4) {
  798.       ifp->hwaddr = mallocw((unsigned)ifp->iftype->hwalen);
  799.       (*ifp->iftype->scan)(ifp->hwaddr,argv[4]);
  800.      }
  801.      ifp->raw = axip_raw;
  802.      ifp->stop = axip_stop;
  803.      ifp->next = Ifaces;
  804.      Ifaces = ifp;
  805.      return 0;
  806. }
  807. static int
  808. axip_stop(iface)
  809. struct iface *iface;
  810. {
  811.      axipaddr[iface->dev] = 0;
  812.      return 0;
  813. }
  814.  
  815. /* raw routine for sending AX.25 on top of IP */
  816. static int
  817. axip_raw(iface,bp)
  818. struct iface *iface;    /* Pointer to interface control block */
  819. struct mbuf *bp;        /* Data field */
  820. {
  821.      int16 len, fcs = 0xffff;
  822.      struct mbuf *bp1;
  823.  
  824.      dump(iface,IF_TRACE_OUT,(unsigned)iface->type,bp);
  825.      iface->rawsndcnt++;
  826.      iface->lastsent = secclock();
  827.      len = len_p(bp);
  828.      if(dup_p(&bp1,bp,0,len) != len) {
  829.       free_p(bp);
  830.       return -1;
  831.      }
  832.      while (len--)        /* calculate FCS */
  833.       fcs = (fcs >> 8) ^ fcstab[(fcs ^ PULLCHAR(&bp1)) & 0x00ff];
  834.  
  835.      fcs ^= 0xffff;    /* final FCS (is this right?) */
  836.      if((bp1 = alloc_mbuf(sizeof (fcs))) == NULLBUF) {
  837.       free_p(bp);
  838.       return -1;
  839.      }
  840.      *bp1->data = fcs & 0xff;
  841.      *(bp1->data+1) = (fcs >> 8) & 0xff;
  842.      bp1->cnt += sizeof(fcs);
  843.      append(&bp,bp1);
  844.      return ip_send(INADDR_ANY,axipaddr[iface->dev],AX25_PTCL,0,0,bp,0,0,0);
  845. }
  846. #endif
  847.  
  848. #endif /* AX25 */
  849.